﻿using UnityEngine;
using UnityEditor;
using System.Collections;
using Unity;

public class PlatformScript : MonoBehaviour {
	public GameObject platforms;
	public GameObject player;
	float diffTime = 2f;
	float startTime = 0.0f;
	float currentTime = 0.0f;

	// Use this for initialization
	void Start () {
		CreatePlatformObjects (platforms, 0.05f, true, -2f);
		CreatePlatformObjects (platforms, 0.05f, true, 0f);
		CreatePlatformObjects (platforms, 0.05f, true, 2f);
	}

	public void CreatePlatformObjects(GameObject parent, float gravity, bool isKinematic, float initialY)
	{
		Platform plt = PlatformCreationScript.plat.GetPlatform ();
		float y = initialY;
		if (initialY == -100f) {
			y = 12.0f;
		}
		float x = 0;
		if (plt.Location == "1") {
			x = 3.0f;
		}
		else if (plt.Location == "2") {
			x = 0;
		}
		else if (plt.Location == "3") {
			x = -3.0f;
		}
		Object platformPrefab = AssetDatabase.LoadAssetAtPath("Assets/Prefabs/Platform.prefab", typeof(GameObject));
		GameObject platform = Instantiate(platformPrefab, new Vector3(x, y, 0), Quaternion.identity) as GameObject;
		platform.rigidbody2D.gravityScale = gravity;
		platform.rigidbody2D.isKinematic = isKinematic;
		//BoxCollider2D bc2d = platform.collider2D as BoxCollider2D;
		//bc2d.size = new Vector3 (3.04f, 0.64f, 0);
		platform.tag="plTag";

		if (plt.Size == "1") {
			platform.transform.localScale = new Vector3(platform.transform.localScale.x * 1, platform.transform.localScale.y, 0);
			//platform.transform.localScale = new Vector3(1, 1, 0);
		}
		else if (plt.Size == "2") {
			platform.transform.localScale = new Vector3(platform.transform.localScale.x * 0.9f, platform.transform.localScale.y, 0);
			//platform.transform.localScale = new Vector3(0.9f, 1, 0);
		}
		else if (plt.Size == "3") {
			platform.transform.localScale = new Vector3(platform.transform.localScale.x * 0.8f, platform.transform.localScale.y, 0);
			//platform.transform.localScale = new Vector3(0.8f, 1, 0);
		}
		else if (plt.Size == "4") {
			platform.transform.localScale = new Vector3(platform.transform.localScale.x * 0.7f, platform.transform.localScale.y, 0);
			//platform.transform.localScale = new Vector3(0.7f, 1, 0);
		}

		platform.transform.parent = parent.transform;
		//PlatformCreationScript.plats
	}

	// Update is called once per frame
	void Update () {

	}

    //public void AddGravity(GameObject[] gg)
    //{
    //    GameObject.FindGameObjectWithTag("Ground").rigidbody2D.gravityScale = 0.05f;
    //    foreach (GameObject item in gg) {
    //        item.rigidbody2D.gravityScale = 0.05f;
    //        item.rigidbody2D.isKinematic = true;
    //        item.rigidbody2D.AddForce((new Vector3(0,-50,15))*20);
    //    }
    //}

	private Vector2 speed = new Vector2 (0, 1f);

	public void MoveRigidBody(GameObject[] gg)
	{
		//GameObject ground = GameObject.FindGameObjectWithTag ("Ground");
		//ground.rigidbody2D.MovePosition(ground.rigidbody2D.position - speed * Time.deltaTime);
		foreach (GameObject item in gg) {
			item.rigidbody2D.MovePosition(item.rigidbody2D.position - speed * Time.deltaTime);
			//Debug.Log(player.transform.position.y + " " + item.rigidbody2D.position.y);
			if (player.transform.position.y > item.rigidbody2D.position.y) {
				BoxCollider2D bc2d = item.AddComponent<BoxCollider2D>();
				//BoxCollider2D bc2d = item.collider2D as BoxCollider2D;
				bc2d.size = new Vector3 (11f, 10f, 0);
			}
			else {
				Destroy(item.collider2D);
			}
		}
	}

	public void CreateAndDestroy(GameObject parent)
	{
		GameObject[] gg = GameObject.FindGameObjectsWithTag ("plTag");
		MoveRigidBody (gg);
		CreatePL (parent);
		foreach (GameObject g in gg) {
			if (g.transform.position.y < -4.0f) {
				GameObject.Destroy(g);
			}
		}
	}

	void CreatePL(GameObject parent)	{
		currentTime = Time.time;
		if (PlatformCreationScript.gameStartTime > 10.0f) {
			PlatformCreationScript.plat.ChangeLevelSize(0.4, 0.3, 0.2, 0);
			PlatformCreationScript.plats = PlatformCreationScript.plat.CreatePlatforms(20);
		}
		if (currentTime - startTime > diffTime) {
			CreatePlatformObjects (platforms, 0.05f, true, -100f);
			startTime = currentTime;
		}
	}
}
